package Count;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class count {
    //统计每个字符串出现的次数
    public static Map<String,Integer> count(String[] words){
        Map<String,Integer> map=new HashMap<>();
        for(String s:words){
            if(map.get(s)==null){
                map.put(s,1);
            }else{
                int val=map.get(s);
                map.put(s,val+1);
            }
        }
        return map;
    }
    public static void main(String[] args){
        String[] words={"hello","abc","hello","world"};
        Map<String,Integer> map=count(words);
        Set<Map.Entry<String,Integer>> set=map.entrySet();
        for(Map.Entry<String,Integer> entry:set){
            System.out.println("key:"+entry.getKey()+" val:"+entry.getValue());
        }
    }

}
